import time
from report import report_sxw


class account_invoice(report_sxw.rml_parse):
    _packing_total = 0.0
    _weight_total = 0.0
    _volume_total = 0.0
    _qty_total = 0.0

    def __init__(self, cr, uid, name, context):
        super(account_invoice, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'time': time,
            'get_data': self.get_data,
            'get_products_name': self.get_products_name,
            'get_category_name': self.get_category_name,
            'get_packing': self.get_packing,
            'get_weight': self.get_weight,
            'get_volume': self.get_volume,
            'get_qty': self.get_qty,
            'get_packing_total': self.get_packing_total,
            'get_weight_total': self.get_weight_total,
            'get_volume_total': self.get_volume_total,
            'get_qty_total': self.get_qty_total,
        })


    def get_data(self, invoice_id, categ_id):
        res = []

        self.cr.execute(""" select sum(ail.quantity) as quantity,sum(ail.price_unit)as price_unit,sum(ail.price_subtotal)as price_subtotal,pt.categ_id from account_invoice_line ail
left join product_template pt on ail.product_id=pt.id
 where invoice_id=%s and pt.categ_id not in (select id from product_category where parent_id=%s) group by pt.categ_id 
union all
 select ail.quantity,ail.price_unit,ail.price_subtotal,pt.categ_id from account_invoice_line ail
left join product_template pt on ail.product_id=pt.id
 where invoice_id=%s and pt.categ_id in (select id from product_category where parent_id=%s)  """,
                        (invoice_id, categ_id, invoice_id, categ_id,))

        res = self.cr.fetchall()
        return res

    def get_products_name(self, invoice_id, categ_id):
        res = []

        self.cr.execute(""" select distinct product_id,pt.name, COALESCE(pt.weight_net,0), COALESCE(pt.volume,0) from account_invoice_line ail 
left join product_template pt on ail.product_id=pt.id 
where invoice_id=%s and pt.categ_id=%s """, (invoice_id, categ_id,))

        res = self.cr.fetchall()

        return res

    def get_category_name(self, invoice_id, categ_id):

        self.cr.execute(""" select distinct product_id,pt.name from account_invoice_line ail 
left join product_template pt on ail.product_id=pt.id 
where invoice_id=%s and pt.categ_id=%s """, (invoice_id, categ_id,))

        res = self.cr.fetchall()

        print len(res)
        if res:
            if len(res) > 1:
                self.cr.execute("select name from product_category where id=%s", (categ_id,))
                result = self.cr.fetchall()
                return result[0][0] or ''
            else:
                return res[0][1] or ''


    def get_weight(self, weight, product_qty):
        b = weight * product_qty
        self._weight_total += b
        return b


    def get_packing(self, product_id, product_qty):

        self.cr.execute(" select id, qty from product_packaging where product_id=%s limit 1 ", (product_id,))
        res = self.cr.fetchall()

        a = product_qty / res[0][1] or 0
        self._packing_total += a
        return a

    def get_volume(self, volume, product_qty):
        c = volume * product_qty
        self._volume_total += c
        return c

    def get_qty(self, qty):
        self._qty_total += qty
        return qty

    def get_packing_total(self):
        return self._packing_total


    def get_weight_total(self):
        return self._weight_total

    def get_volume_total(self):
        return self._volume_total

    def get_qty_total(self):
        return self._qty_total


report_sxw.report_sxw(
    'report.account.invoice.add',
    'account.invoice',
    'addons/invoice_custom/report/account_print_invoice_add.rml',
    parser=account_invoice
)
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:




 
 

